import {GCtrl} from "../../lib/util/GCtrl";
import FPJDefine from "../../Game/FPJ/Prefabs/FPJDefine";


const {ccclass, property} = cc._decorator;

@ccclass
export default class FPJ_FriendListItem extends cc.Component {

    @property(cc.Sprite)
    HeadIcon: cc.Sprite = null;

    @property(cc.Label)
    LbName: cc.Label = null;

    @property(cc.Label)
    LbState: cc.Label = null;

    @property(cc.Node)
    BtnTalkNode: cc.Node = null;

    @property(cc.Node)
    BtnLookNode: cc.Node = null;

    _info = null;
    _friendListCom = null;
    itemID = null;

    private isHall = true;

    start() {
        cc.systemEvent.on("ENTER_HALL",this.setIsHall.bind(this) );
    }
    onDestroy(){
        cc.systemEvent.off("ENTER_HALL",this.setIsHall.bind(this) );
    }

    //设置是否在大厅
    setIsHall(isHall){
        this.isHall = isHall;
    }

    init(itemID,info,friendListCom){
        this.itemID = itemID;
        this._info = info;
        this._friendListCom = friendListCom;
        this.initUI();
    }

    initUI(){
        this.LbName.string = this._info["rolename"] || "未知";
        this.LbState.string = this._info["play_now"] ? "游戏中":"大厅";

        this.BtnTalkNode.active = true;//this._info["canTalk"];
        this.BtnLookNode.active = this._info["play_now"] || false;

        GCtrl.setHeadSpr(this._info["logo"], this.HeadIcon);
    }

    btnClickTalk(){

        let isHall = true; //根据数据判断是在大厅还是在座子上
        let tableId = "";
        let talkParam = {
            isHall:isHall,
            chatId: isHall ? cc.vv.session.getParentRid() : tableId ,
            private: {
                menuName: this._info["rolename"] ,
                chatId: this._info["rid"]
            }
        };
        cc.vv.UIMgr.open(cc.vv.GLConfig.DWC_DetailChat_path ,null,null,talkParam);

        GCtrl.UIMgr.close(GCtrl.GLConfig.DWC_FriendList_path, GCtrl.UIMgr.closeType.hide);

    }

    btnClickLook(){
        cc.log("btnClickLook")

        let tables_id = this._info["play_now"];
        let gameId = cc.vv.GLDefine.EGameID.FPJ;

        cc.vv.GLUtils.sendRequest("ATTEnterTable", {
            table_id: tables_id,
        }, (error, data) => {
            if (!error) {
                if(data.errorcode){
                    cc.vv.GLUtils.showTips(data.errordes);
                }else{
                    FPJDefine.setTableDetail(data.table_data);
                    FPJDefine.setcurrentTableId(data.table_data.table_id)
                    FPJDefine.setCurrentState(data.table_data.status)
                    let _path = cc.vv.GLDefine.getEnglishNameById(gameId);
                    cc.vv.UIMgr.open(cc.vv.GLConfig.waiting_path);
                    cc.vv.sceneMgr.openScene(_path, data, () => {
                        cc.vv.eventCustom.emit("openGame");
                        cc.vv.UIMgr.close(cc.vv.GLConfig.waiting_path, cc.vv.UIMgr.closeType.hide);
                    });
                    //关闭
                    this._friendListCom.btnClickClose();
                }
            }
        });
    }

}
